Completed
Pull Request — master (#99)
by Johan
01:15
created

EAN13.js ➔ ???   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 39
rs 8.8571
c 6
b 0
f 0
1
// Encoding documentation:
2
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
3
4
import EANencoder from './ean_encoder.js';
5
6
class EAN13{
7
	constructor(string, options){
8
		// Add checksum if it does not exist
9
		if(string.search(/^[0-9]{12}$/) !== -1){
10
			this.string = string + this.checksum(string);
11
		}
12
		else{
13
			this.string = string;
14
		}
15
16
		this.displayValue = options.displayValue;
17
18
		// Define the EAN-13 structure
19
		this.structure = [
20
			"LLLLLL",
21
			"LLGLGG",
22
			"LLGGLG",
23
			"LLGGGL",
24
			"LGLLGG",
25
			"LGGLLG",
26
			"LGGGLL",
27
			"LGLGLG",
28
			"LGLGGL",
29
			"LGGLGL"
30
		];
31
32
		// Make sure the font is not bigger than the space between the guard bars
33
		if(options.fontSize > options.width * 10){
34
			this.fontSize = options.width * 10;
35
		}
36
		else{
37
			this.fontSize = options.fontSize;
38
		}
39
40
		// Make the guard bars go down half the way of the text
41
		this.guardHeight = options.height + this.fontSize / 2 + options.textMargin;
42
43
		// Adds a last character to the end of the barcode
44
		this.lastChar = options.lastChar;
45
	}
46
47
	valid(){
48
		return this.string.search(/^[0-9]{13}$/) !== -1 &&
49
			this.string[12] == this.checksum(this.string);
50
	}
51
52
	encode(){
53
		var encoder = new EANencoder();
54
		var result = [];
55
56
		var structure = this.structure[this.string[0]];
57
58
		// Get the string to be encoded on the left side of the EAN code
59
		var leftSide = this.string.substr(1, 6);
60
61
		// Get the string to be encoded on the right side of the EAN code
62
		var rightSide = this.string.substr(7, 6);
63
64
		// Add the first digigt
65
		if(this.displayValue){
66
			result.push({
67
				data: "000000000000",
68
				text: this.string[0],
69
				options: {textAlign: "left", fontSize: this.fontSize}
70
			});
71
		}
72
73
		// Add the guard bars
74
		result.push({
75
			data: "101",
76
			options: {height: this.guardHeight}
77
		});
78
79
		// Add the left side
80
		result.push({
81
			data: encoder.encode(leftSide, structure),
82
			text: leftSide,
83
			options: {fontSize: this.fontSize}
84
		});
85
86
		// Add the middle bits
87
		result.push({
88
			data: "01010",
89
			options: {height: this.guardHeight}
90
		});
91
92
		// Add the right side
93
		result.push({
94
			data: encoder.encode(rightSide, "RRRRRR"),
95
			text: rightSide,
96
			options: {fontSize: this.fontSize}
97
		});
98
99
		// Add the end bits
100
		result.push({
101
			data: "101",
102
			options: {height: this.guardHeight}
103
		});
104
105
		if(this.lastChar && this.displayValue){
106
			result.push({data: "00"});
107
108
			result.push({
109
				data: "00000",
110
				text: this.lastChar,
111
				options: {fontSize: this.fontSize}
112
			});
113
		}
114
115
		return result;
116
	}
117
118
	// Calulate the checksum digit
119
	// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
120
	checksum(number){
121
		var result = 0;
122
123
		var i;
124
		for(i = 0; i < 12; i += 2){
125
			result += parseInt(number[i]);
126
		}
127
		for(i = 1; i < 12; i += 2){
128
			result += parseInt(number[i]) * 3;
129
		}
130
131
		return (10 - (result % 10)) % 10;
132
	}
133
}
134
135
export default EAN13;
136